<?php

namespace Forms;

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Text,
    Phalcon\Forms\Element\Select,
    Phalcon\Forms\Element\Date,
    Phalcon\Forms\Element\Numeric,
    Phalcon\Validation\Message,
    Phalcon\Validation\Message\Group,
    Phalcon\Forms\Element\Hidden,
    Phalcon\Forms\Element\Password,
    Phalcon\Forms\Element\Submit,
    Phalcon\Forms\Element\Check,
    Phalcon\Validation\Validator\PresenceOf,
    Phalcon\Validation\Validator\Email,
    Phalcon\Validation\Validator\Identical,
    Phalcon\Validation\Validator\StringLength,
    Phalcon\Validation\Validator\Confirmation;

/**
 * validate and print render Frontend login form
 */
class LoginForm extends Form {

    public function initialize() {
        $identity = $this->auth->getIdentity();
        //username
        $username = new Text('username', array(
            'placeholder' => 'username',
        ));

        if ($identity) {
            $username->setDefault($identity['username']);
        }

        $username->addValidators(array(
            new PresenceOf(array(
                'message' => 'Tài khoản không được rỗng'
                    ))
        ));

        $this->add($username);

        //Password
        $password = new Password('password', array(
            'placeholder' => 'Password'
        ));

        $password->addValidator(
                new PresenceOf(array(
            'message' => 'Mật khẩu không được rỗng'
                ))
        );

        $this->add($password);

        //Remember
        $remember = new Check('remember', array(
            'value' => 'yes'
        ));

        $remember->setLabel('Remember me');

        $this->add($remember);

        //CSRF
        $csrf = new Hidden('csrf');

        $csrf->addValidator(
                new Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
                ))
        );

        $this->add($csrf);

        $this->add(new Submit('go', array(
            'class' => 'btn btn-primary btn-large'
        )));
    }

    /**
     * Prints messages for a specific element
     */
    public function messages($name) {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }

    /**
     * 
     * Add a message to form
     */
    public function appendMessage($message, $field, $type = null) {
        if (is_string($message)) {
            $message = new Message($message, $field, $type);
        }

        if ($message instanceof Message || $message instanceof ModelMessage) {
            // Check if there is a group for the field already.
            if (!is_null($this->_messages) && array_key_exists($field, $this->_messages)) {
                $this->_messages[$field]->appendMessage($message);
            } else {
                $this->_messages[$field] = new Group(array($message));
            }
        } else {
            throw new Exception("Can't append message into the form, invalid type.");
        }
    }

}
